<?php
session_start();
defined('IN_PHPCMS') or exit('No permission resources.');
class index {
	private $setting, $catid, $contentid, $siteid, $mood_id;
	public function __construct() {
		$this->setting = getcache('mood_program', 'commons');

		$this->mood_id = isset($_GET['id']) ? $_GET['id'] : '';
		if(!preg_match("/^[a-z0-9_\-]+$/i",$this->mood_id)) showmessage((L('illegal_parameters')));
		if (empty($this->mood_id)) {
			showmessage(L('id_cannot_be_empty'));
		}
		list($this->catid, $this->contentid, $this->siteid) = id_decode($this->mood_id);
		
		$this->setting = isset($this->setting[$this->siteid]) ? $this->setting[$this->siteid] : array();
		
		foreach ($this->setting as $k=>$v) {
			if (empty($v['use'])) unset($this->setting[$k]);
		}
		define('SITEID', $this->siteid);
	}

	//显示心情
	public function init() {
		$mood_id =& $this->mood_id;
		$setting =& $this->setting;
		$mood_db = pc_base::load_model('mood_model');
		$data = $mood_db->get_one(array('catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid));
		foreach ($setting as $k=>$v) {
			$setting[$k]['fields'] = 'n'.$k;
			if (!isset($data[$setting[$k]['fields']])) $data[$setting[$k]['fields']] = 0;
			if (isset($data['total']) && !empty($data['total'])) {
				$setting[$k]['per'] = ceil(($data[$setting[$k]['fields']]/$data['total']) * 60);
			} else {
				$setting[$k]['per'] = 0;
			}
		}
		ob_start();
		include template('mood', 'index');
		$html = ob_get_contents();
		ob_clean();
		echo format_js($html);
	}
	// 取得总票数
	public function total() {
		$mood_db = pc_base::load_model('mood_model');
		$data = $mood_db->get_one(array('catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid));
		if (isset($data['total']) && !empty($data['total'])) {
			$total = $data['total'];
		} else {
			$total = "0";
		}
		exit(trim_script($_GET['callback']).'('.json_encode(array('total'=>$total)).')');
	}
	//提交选中
	public function post() {
		//$mood_id =& $this->mood_id;
		//$setting =& $this->setting;
		//$cookies = param::get_cookie('mood_id');
		//$cookie = explode(',', $cookies);

        $qaptchaid = $_GET['qaptchaid'];
        $generatepass = $_GET['generatepass'];

        if($qaptchaid != "" && $generatepass != "")
        {
           if($_SESSION[$qaptchaid] != $generatepass)
           {
             $this->_show_result(-1, "验证码不正或已经过期！");
           }
        } else {
           $this->_show_result(-1, "请先滑动滚动条,再进行投票！");
        }

		// 投票活动是否结束判断
		$announce_db = pc_base::load_model('announce_model');
		if($overdue = $announce_db->get_one())
		{
			// 过期判断
			if(strtotime(date("Y-m-d"))>strtotime($overdue['endtime']))
			{
				$this->_show_result(-1, "已经结束投票");
			}
		}
		
		// 取得投票者IP
		if ($_SERVER["HTTP_X_FORWARDED_FOR"]) {
			
     		$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
     		
		} else {
		    $ip = $_SERVER["REMOTE_ADDR"];
		}
		
		// 取得该作品数据
		$vote_data_db = pc_base::load_model('vote_data_model');
		$where = "`subjectid`='".$qaptchaid."' AND `ip`='".$ip."'";					
		$ip_data = $vote_data_db->select($where, 'subjectid,ip');
			
		if (!empty($ip_data)) {
			$this->_show_result(-1, "您已经投过票了");	
		}else {
			
			// 取得当前日期
			$now = date("Y-m-d   h:i:s");
			
			// 插入投票IP数据
			$vote_data_db->insert(array('subjectid'=>$qaptchaid,'time'=>$now,'ip'=>$ip));
			
			// 更新作品投票数 			
			$mood_db = pc_base::load_model('mood_model');
			$activity_vote_db = pc_base::load_model('activity_vote_model');
		    
			$key = isset($_GET['k']) && intval($_GET['k']) ? intval($_GET['k']) : '';
			$fields = 'n'.$key;


			if ($data = $mood_db->get_one(array('catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid))) {
				$mood_db->update(array('total'=>'+=1', $fields=>'+=1', 'lastupdate'=>$now), array('id'=>$data['id']));
				$data['total']++;
				$data[$fields]++;
			    $activity_vote_db->update(array('count'=>$data['total'], 'updatetime'=>$now), array('id'=>$this->contentid));
			} else {
				$mood_db->insert(array('total'=>'1', $fields=>'1', 'catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid,'
				lastupdate'=>$now));
				$data['total'] = 1;
				$data[$fields] = 1;
			    $activity_vote_db->update(array('count'=>$data['total'], 'updatetime'=>$now), array('id'=>$this->contentid));
			}



			//param::set_cookie('mood_id', $cookies.','.$mood_id);
			//foreach ($setting as $k=>$v) {
			//	$setting[$k]['fields'] = 'n'.$k;
			//	if (!isset($data[$setting[$k]['fields']])) $data[$setting[$k]['fields']] = 0;
			//	if (isset($data['total']) && !empty($data['total'])) {
			//		$setting[$k]['per'] = ceil(($data[$setting[$k]['fields']]/$data['total']) * 60);
			//	} else {
			//		$setting[$k]['per'] = 0;
			//	}
			//}

			//ob_start();
		//	include template('mood', 'index');
		//	$html = ob_get_contents();
		//	ob_clean();
		//	$this->_show_result(1,$html);
	    	$this->_show_result(1,$data['total']);
		}
	}
	
	//显示AJAX结果
	protected function _show_result($status = 0, $msg = '') {
		if(CHARSET != 'utf-8') {
			$msg = iconv(CHARSET, 'utf-8', $msg);
		}
		exit(trim_script($_GET['callback']).'('.json_encode(array('status'=>$status, 'data'=>$msg)).')');
	}
}